package com.ssy.lingxi.system.controller;

import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.PageModel;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.sms.api.common.AliConfigEnum;
import com.ssy.lingxi.sms.api.common.ServiceTypeEnum;
import com.ssy.lingxi.sms.api.common.SmsTemplateEnum;
import com.ssy.lingxi.sms.api.common.TencentConfigEnum;
import com.ssy.lingxi.sms.api.feign.IConfigSmsFeign;
import com.ssy.lingxi.sms.api.model.response.MapResponse;
import com.ssy.lingxi.system.dto.request.*;
import com.ssy.lingxi.system.dto.response.MessageTemplateTypeAllResponse;
import com.ssy.lingxi.system.entity.CommonParameters;
import com.ssy.lingxi.system.entity.TemplateParameters;
import com.ssy.lingxi.system.service.CommonParametersService;
import com.ssy.lingxi.system.service.TemplateParametersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
/**
 * @menu 基础设置-短信平台设置
 * @author wrc
 * @date 2020/7/20
 */
@Api(tags = "短信平台")
@RestController
public class MessageController {

    @Resource
    CommonParametersService commonParametersService;

    @Resource
    TemplateParametersService templateParametersService;

    @Resource
    IConfigSmsFeign iSmsServiceFeign;

    /**
     * 查询当前配置的服务商类型
     * @return 服务商类型: 1-腾讯云，2-阿里云
     */
    @RequestMapping(value = "/message/common/getServiceType", method = RequestMethod.GET)
    public Wrapper<Integer> getServiceType() {
        return iSmsServiceFeign.getServiceType();
    }

    /**
     * 查询参数代码
     * @param serviceType 服务商类型: 1-腾讯云，2-阿里云
     * @return SmsConfig
     */
    @RequestMapping(value = "/message/common/getParamCode", method = RequestMethod.GET)
    public Wrapper<List<MapResponse>> getParamCode(@RequestParam("serviceType") Integer serviceType) {
        if(serviceType.equals(ServiceTypeEnum.Tencent.getCode())){
            return Wrapper.success(TencentConfigEnum.getCodes());
        }else if(serviceType.equals(ServiceTypeEnum.Ali.getCode())){
            return Wrapper.success(AliConfigEnum.getCodes());
        }
        return Wrapper.success();
    }

    /**
     * 查询短信公共参数列表
     * @param serviceType 服务商类型: 1-腾讯云，2-阿里云
     * @return 短信公共参数列表
     */
    @ApiOperation(value = "短信公共参数列表")
    @GetMapping("/message/common/list")
    public Wrapper<PageData<CommonParameters>> messageCommonList(PageModel request, @RequestParam("serviceType") Integer serviceType) {
        return commonParametersService.messageCommonList(request, serviceType);
    }

    @ApiOperation(value = "添加短信公共参数")
    @PostMapping("/message/common/add")
    public Wrapper messageCommonAdd(@RequestBody  CommonParametersAddRequest request,HttpServletRequest httpServletRequest) {
        return commonParametersService.messageCommonAdd(httpServletRequest,request);
    }


    @ApiOperation(value = "修改短信公共参数")
    @PostMapping("/message/common/update")
    public Wrapper messageCommonUpdate(@RequestBody CommonParametersUpdateRequest request) {
        return commonParametersService.messageCommonUpdate(request);
    }


    @ApiOperation(value = "删除短信公共参数")
    @PostMapping("/message/common/delete")
    public Wrapper messageCommonDelete(@RequestBody CommonParametersDeteleRequest request) {
        return commonParametersService.messageCommonDelete(request);
    }


    /**
     * 查询功能模块代码
     * @return SmsConfig
     */
    @RequestMapping(value = "/message/template/getModuleCode", method = RequestMethod.GET)
    public Wrapper<List<MapResponse>> getModuleCode() {
        return Wrapper.success(SmsTemplateEnum.getCodes());
    }

    @ApiOperation(value = "短信模板参数列表")
    @GetMapping("/message/template/list")
    public Wrapper<PageData<TemplateParameters>> messageTemplateList(PageModel request) {
        return templateParametersService.messageTemplateList(request);
    }

    @ApiOperation(value = "添加短信模板参数")
    @PostMapping("/message/template/add")
    public Wrapper messageTemplateAdd(@RequestBody TemplateParametersAddRequest request,HttpServletRequest httpServletRequest) {
        return templateParametersService.messageTemplateAdd(httpServletRequest,request);
    }


    @ApiOperation(value = "修改短信模板参数")
    @PostMapping("/message/template/update")
    public Wrapper messageTemplateUpdate(@RequestBody TemplateParametersUpdateRequest request) {
        return templateParametersService.messageTemplateUpdate(request);
    }


    @ApiOperation(value = "删除短信模板参数")
    @PostMapping("/message/template/delete")
    public Wrapper messageCommonDelete(@RequestBody TemplateParametersDeteleRequest request) {
        return templateParametersService.messageTemplateDelete(request);
    }


    @ApiOperation(value = "短信模板参数详情")
    @GetMapping("/message/template/details")
    public Wrapper<TemplateParameters> messageTemplateDetails(TemplateParametersDetailsRequest request) {
        return templateParametersService.messageTemplateDetails(request);
    }


    @ApiOperation(value = "短信模板类型列表查询")
    @GetMapping("/message/template/type/all")
    public Wrapper<List<MessageTemplateTypeAllResponse>> messageTemplateTypeAll() {
        return templateParametersService.messageTemplateTypeAll();
    }

    /**
     * 清空短信内容模板
     * @return 是否成功
     */
    @RequestMapping(value = "/message/template/clearSmsTemplate", method = RequestMethod.POST)
    public Wrapper<Boolean> clearSmsTemplate() {
        return Wrapper.success(templateParametersService.clearSmsTemplate());
    }
}
